<div class="layuimini-main">

    <div class="layui-form layuimini-form" lay-filter = "formTest">
        <input type="hidden" name="id" value="{{ $data['info']['id'] }}">
        <div class="layui-form-item">
            <label class="layui-form-label">上级分类</label>
            <div class="layui-input-block">
                <label>
                    <select name="pid" lay-filter="pid">
                        <option value='0'>顶级分类</option>
                        @foreach($data['parent'] as $v)

                            <option value="{{ $v['id'] }}">{{ $v['name'] }}</option>
                            @if(!empty($v['child']))
                                @foreach($v['child'] as $vv )
                                    <option value="{{ $vv['id'] }}"><span>|----</span>{{ $vv['name'] }}</option>
                                @endforeach
                            @endif
                        @endforeach
                    </select>
                </label>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">绑定模型</label>
            <div class="layui-input-block">
                <label>
                    <select name="model_id" lay-filter="model_id">
                        <option value='0'>无绑定</option>
                        @foreach($data['model'] as $v)
                            <option value="{{ $v->id }}">{{ $v->name }}</option>
                        @endforeach
                    </select>
                </label>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" lay-reqtext="请输入分类名称"
                       autocomplete="off" placeholder="请输入分类名称" value="{{ $data['info']['name'] }}" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input type="number" name="order" placeholder="请输入1-100数字" value="{{ $data['info']['order'] }}" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">SEO 关键词</label>
            <div class="layui-input-block">
                <input type="text" name="keywords" lay-verify="required" lay-reqtext="请输入关键词"
                       autocomplete="off" placeholder="请输入关键词" value="{{ $data['info']['keywords'] }}" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label required">SEO描述</label>
            <div class="layui-input-block">
                <textarea name="description" class="layui-textarea" placeholder="请输入SEO描述">{{ $data['info']['description'] }}</textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;
        var miniPage = layui.miniPage;
        /**
         * 选择框赋值
         */
        form.val('formTest',{
            "pid" : {{ $data['info']['pid'] }},
            "model_id" : {{ $data['info']['model_id'] }}
        })
        form.render("select")

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                url: 'categories/' + data.field.id,
                method: 'PUT',
                data: data.field,
                success: function (res) {
                    console.log(res)
                    if (res.code === 0){
                        layer.msg('保存成功',{icon:1,time:2000},function () {
                            // 关闭弹出层
                            layer.close(parentIndex);
                            var options = {};
                            options.homeInfo = options.homeInfo || {};
                            miniPage.refresh(options);
                        })
                    } else{
                        layer.msg( res.data,{icon: 5})
                    }
                },
                error:function (res) {
                    layer.msg( '请求错误',{icon: 5})
                }
            });
            return false;
        });
    });
</script>
